{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表转numpy的array\n",
    "a = [1, 2, 3, 4]\n",
    "np.array(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 2, 3]), (4,), array([1, 2, 3, 4]), (4,))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.arange(4)\n",
    "arr2 = np.arange(1, 5)  # (开始,截止,步长)\n",
    "arr1, arr1.shape, arr2, arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  1.,  26.,  51.,  76., 101.]),\n",
       " (5,),\n",
       " array([ 1., 21., 41., 61., 81.]),\n",
       " (5,))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 区间内的等间距数组\n",
    "arr1 = np.linspace(1, 101, 5)\n",
    "# 不包括右区间的值\n",
    "arr2 = np.linspace(1, 101, 5, endpoint=False)\n",
    "arr1, arr1.shape, arr2, arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([  1.        ,   3.16227766,  10.        ,  31.6227766 ,\n",
       "        100.        ]),\n",
       " (5,))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取对数后的等距插值 10^0=1 10^2=100\n",
    "arr = np.logspace(0, 2, 5)\n",
    "arr, arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 1, 2, 3, 4, 5, 6, 7, 8]),\n",
       " (9,),\n",
       " array([[0, 1, 2],\n",
       "        [3, 4, 5],\n",
       "        [6, 7, 8]]),\n",
       " (3, 3),\n",
       " array([[0, 1, 2],\n",
       "        [3, 4, 5],\n",
       "        [6, 7, 8]]),\n",
       " (3, 3),\n",
       " array([[0, 1, 2],\n",
       "        [3, 4, 5],\n",
       "        [6, 7, 8]]),\n",
       " (3, 3))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数组转化为矩阵\n",
    "arr = np.arange(9)\n",
    "arr1 = arr.reshape(3, 3)\n",
    "arr2 = arr.reshape(3, -1)\n",
    "arr3 = arr.reshape(-1, 3)\n",
    "arr, arr.shape, arr1, arr1.shape, arr2, arr2.shape, arr3, arr3.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0., 0.],\n",
       "        [0., 0.]]),\n",
       " (2, 2),\n",
       " array([[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]]),\n",
       " (2, 4))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化矩阵\n",
    "arr1 = np.zeros((2, 2))\n",
    "arr2 = np.ones((2, 4))\n",
    "arr1, arr1.shape, arr2, arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[-inf, -inf],\n",
       "        [-inf, -inf]]),\n",
       " (2, 2),\n",
       " array([[1, 2],\n",
       "        [1, 2]]),\n",
       " (2, 2))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.full((2, 2), -np.inf)\n",
    "arr2 = np.full((2, 2), [1, 2])\n",
    "arr1, arr1.shape, arr2, arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2, 3]]), array([[1, 1, 1]]), array([[1., 1., 1.]]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1, 2, 3]])\n",
    "arr1 = np.ones_like(x)\n",
    "arr2 = np.ones(x.shape)\n",
    "x, arr1, arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1., 0., 0.],\n",
       "        [0., 1., 0.],\n",
       "        [0., 0., 1.]]),\n",
       " array([[1., 0., 0., 0., 0.],\n",
       "        [0., 1., 0., 0., 0.],\n",
       "        [0., 0., 1., 0., 0.]]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对角线为1的矩阵\n",
    "arr1 = np.identity(3)\n",
    "arr2 = np.eye(3, M=5)\n",
    "arr1, arr2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('d2l')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6bdcd3dff61cfecb093f68bfbd67338d4a9739a04d65268bf3b66287b86a0a9e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
